using System.Collections.Generic;
using System.Web.UI;
using System.ComponentModel;

namespace Library.WebControls.SimpleTab {
	[PersistenceMode(PersistenceMode.InnerProperty)]
	[Bindable(false)]
	[ToolboxItem(false)]
	[PersistChildren(false)]
	[ParseChildren(false)]
	public class SpTabPageCollection : List<SpTabPage> {
		private readonly SpTabControl _tabControl;
		internal SpTabPageCollection(SpTabControl tabControl) {
			_tabControl = tabControl;
		}

		public SpTabPage FindTab(string name) {
			foreach (var tab in this) {
				if (tab.Name == name) return tab;
			}
			return null;
		}

		protected void AddNewItem(SpTabPage item, int index) {
			var aMenu = item.TabMenu;
			item.TabControl = _tabControl;
			index++;
			_tabControl.DivTitleContent.Controls.AddAt(index, aMenu);
			_tabControl.Tab.Controls.AddAt(index, item.TabContain);
		}
		protected void AddNewItem(SpTabPage item) {
			var aMenu = item.TabMenu;
			item.TabControl = _tabControl;
			_tabControl.DivTitleContent.Controls.Add(aMenu);
			_tabControl.Tab.Controls.Add(item.TabContain);
		}

		public new void Add(SpTabPage item) {
			AddNewItem(item);
			base.Add(item);
		}

		public new void Clear() {
			base.Clear();
			_tabControl.DivTitleContent.Controls.Clear();
			_tabControl.Tab.Controls.Clear();
		}



		public new bool Remove(SpTabPage item) {
			bool result = base.Remove(item);
			if (result) {
				_tabControl.DivTitleContent.Controls.Remove(item.TabMenu);
				_tabControl.Tab.Controls.Remove(item.TabContain);

			}
			return result;
		}



		public bool IsReadOnly {
			get { return false; }
		}



		public new void Insert(int index, SpTabPage item) {
			AddNewItem(item, index);
			base.Insert(index, item);
		}

		public new void RemoveAt(int index) {
			var item = this[index];
			if (item != null) {
				Remove(item);
			}
		}
	}
}
